/*
 * http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
 */

package android.bluebox.model;

import java.util.ArrayList;

import android.bluebox.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class WorkspaceAdapter extends BaseAdapter {

	private static ArrayList<Workspace> workspaceList;

	private LayoutInflater mInflater;

	public WorkspaceAdapter(Context context, ArrayList<Workspace> list) {
		workspaceList = list;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return workspaceList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return workspaceList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.workspace_row_view, null);
			holder = new ViewHolder();

			holder.txtName = (TextView) convertView.findViewById(R.id.WorkspaceName);
			holder.txtLastVisit = (TextView) convertView.findViewById(R.id.wsl_i_last_visit);
			holder.txtHostIP = (TextView) convertView.findViewById(R.id.wsl_i_host_ip);
			holder.txtDistance = (TextView) convertView.findViewById(R.id.wsl_i_distance);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}


		holder.txtName.setText(workspaceList.get(position).getName());
		holder.txtLastVisit.setText(workspaceList.get(position).getLastVisited());
		holder.txtHostIP.setText(workspaceList.get(position).getHostIP());

		int distance = (int) workspaceList.get(position).getDistance();
		if (distance < 1000000) {
			holder.txtDistance.setText(distance + " m");
		}

		return convertView;
	}

	static class ViewHolder {
		TextView txtName;
		TextView txtLastVisit;
		TextView txtHostIP;
		TextView txtDistance;
	}

	public void setArraylist(ArrayList<Workspace> list) {
		workspaceList = list;
	}
}
